

package com.teaching.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.common.Result;

import com.teaching.dto.NoticeDTO;
import com.teaching.entity.Notice;
import com.teaching.mapper.NoticeMapper;
import com.teaching.service.NoticeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 通知公告
 *
 * @author xubb
 * @date 2021-03-22 15:29:06
 */
@RestController
@RequestMapping("/notice")
@Api(value = "notice", tags = "通知公告管理")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private NoticeMapper noticeMapper;

    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/getNoticeList")
    public Result<IPage<Notice>> getNoticeList(@RequestBody NoticeDTO dto) {
        Page<Notice> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        Notice notice = new Notice();
        BeanUtil.copyProperties(dto, notice);
        return Result.success(noticeMapper.getNoticeList(page, notice));
    }


    /**
     * 通过id查询
     *
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @PostMapping("/getNoticeInfo")
    public Result getNoticeInfo(@RequestBody Notice notice) {
        Notice note = noticeService.getById(notice.getId());
        if (note.getReadCount() == null) {
            note.setReadCount(0);
        } else {
            note.setReadCount(note.getReadCount() + 1);
        }
        noticeService.save(note);
        return Result.success(note);
    }

    /**
     * 新增
     *
     * @param notice
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody Notice notice) {
        return Result.success(noticeService.save(notice));
    }

    /**
     * 修改
     *
     * @param notice
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody Notice notice) {

        return Result.success(noticeService.updateById(notice));
    }

    /**
     * 通过id删除
     *
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @PostMapping("/removeById")
    public Result removeById(@RequestBody Notice notice) {
        return Result.success(noticeService.removeById(notice.getId()));
    }


}
